﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;


namespace HOOMT
{
    class RMainFormToFormDBO : SqliteDataBaseOperator
    {
        public int AddRelations(MainForm mainForm)
        {
            this.DeleteRelations(mainForm);
            if (mainForm.modelFormList != null)

                foreach (OriginalModelForm omf in mainForm.modelFormList)
                    this.AddRelation(mainForm, omf);
            this.CloseConnection();
            return mainForm.modelFormList.Count;
        }

        public bool AddRelation(MainForm mainForm, OriginalModelForm omf)
        {
            try
            {
                /*this.DeleteRelation(mainForm);*/
                SQLiteCommand cmd1 = this.GetCmd();
                cmd1.CommandText = "insert into  r_mainform_form(id,mainformid,formid,formType) values (@id,@mainformid,@formid,@formType)";
                cmd1.Parameters.Add(new SQLiteParameter("id", System.Guid.NewGuid()));
                cmd1.Parameters.Add(new SQLiteParameter("mainformid", mainForm.id));
                cmd1.Parameters.Add(new SQLiteParameter("formid", omf.id));
                cmd1.Parameters.Add(new SQLiteParameter("formType", omf.GetType().ToString()));
                //添加每个FORM
                OriginalModelFormDBO dbo = new OriginalModelFormDBO();
                dbo.AddForm(omf);


                //if ("ObjectModelForm".Equals(this.GetSimpleType(omf.GetType().ToString())))
                //{
                //    ObjectModelFormDBO dbo = new ObjectModelFormDBO();
                //    dbo.AddObjectModelForm((ObjectModelForm)omf);
                //} 
                //else
                //{
                //}
                cmd1.ExecuteNonQuery();
                this.CloseConnection();
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }

        }

        public int DeleteRelations(MainForm mainForm)
        {
            try
            {
                SQLiteCommand cmd1 = this.GetCmd();
                cmd1.CommandText = "delete from  r_mainform_form where mainformid=@mainformid";
                cmd1.Parameters.Add(new SQLiteParameter("mainformid", mainForm.id));
                int i = cmd1.ExecuteNonQuery();
                foreach(OriginalModelForm omf in mainForm.modelFormList)
                {

                    OriginalModelFormDBO dbo = new OriginalModelFormDBO();
                    dbo.DeleteForm(omf);
                    //if ("ObjectModelForm".Equals(this.GetSimpleType(omf.GetType().ToString())))
                    //{
                    //    ObjectModelFormDBO dbo = new ObjectModelFormDBO();
                    //    dbo.Delete(omf);
                    //} 
                    //else
                    //{
                    //}
                }
                return i;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return 0;

            }
            finally
            {
                this.CloseConnection();
            }

        }

        public List<OriginalModelForm> GetRelations(string mainFormId,MainForm thisMainForm)
        {
            
            try
            {
                List<OriginalModelForm> list = new List<OriginalModelForm>();
                SQLiteCommand cmd = GetCmd();
                cmd.CommandText = "select * from r_mainform_form where mainformid=@mainformid";
                cmd.Parameters.Add(new SQLiteParameter("mainformid", mainFormId));
                SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    String formId = reader["formId"].ToString();
                    OriginalModelFormDBO fdbo=new OriginalModelFormDBO();
                    list.Add(fdbo.GetFormById(formId,thisMainForm));
                }

                return list;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;

            }
            finally
            {
                this.CloseConnection();
                
            }
        }
    }
}
